package sinhalatransliterartor;

import java.util.regex.Pattern;

/**
 * This class contains the method <b>transliterate()</b> which transliterate a given string
 * in to Sinhala language.
 * <br><b>
 * Credits:
 * </b></br>
 * The algorithm (transliterate() method) for this Transliteration class
 * is inherited from the Unicode Real time font conversion utility by
 * Language Technology Research Laboratory - University of Colombo School of Computing
 * <br>Reference: <a href="http://www.ucsc.cmb.ac.lk/ltrl/services/feconverter/t1.html"target="_blank">Unicode Real time font conversion utility </a>
 * </br>
 * 
 *
 * @author SUMEDHA
 */
public class Transliterator {

    String text;
    int nVowels;
    Pattern lastPattern;
    String[] consonants = new String[46];
    String[] consonantsUni = new String[46];
    String[] vowels = new String[26];
    String[] vowelsUni = new String[26];
    String[] vowelModifiersUni = new String[26];
    String[] specialConsonants = new String[6];
    String[] specialConsonantsUni = new String[6];
    String[] specialCharUni = new String[2];
    String[] specialChar = new String[2];

    public Transliterator() {
        //invoking the method to initialize the variables that holds characters
        initialize();
    }

    /**
     * Initialization of variables that holds characters
     */
    public final void initialize() {

        vowelsUni[0] = "\u0D8C";
        vowels[0] = "oo";
        vowelModifiersUni[0] = "\u0DD6";
        vowelsUni[1] = "\u0D95";
        vowels[1] = "oe";
        vowelModifiersUni[1] = "\u0DDD";
        vowelsUni[2] = "\u0D95";
        vowels[2] = "o\\)";
        vowelModifiersUni[2] = "\u0DDD";
        vowelsUni[3] = "\u0D86";
        vowels[3] = "aa";
        vowelModifiersUni[3] = "\u0DCF";
        vowelsUni[4] = "\u0D86";
        vowels[4] = "a\\)";
        vowelModifiersUni[4] = "\u0DCF";
        vowelsUni[5] = "\u0D88";
        vowels[5] = "Aa";
        vowelModifiersUni[5] = "\u0DD1";
        vowelsUni[6] = "\u0D88";
        vowels[6] = "ae";
        vowelModifiersUni[6] = "\u0DD1";
        vowelsUni[7] = "\u0D88";
        vowels[7] = "A\\)";
        vowelModifiersUni[7] = "\u0DD1";
        vowelsUni[8] = "\u0D8A";
        vowels[8] = "ii";
        vowelModifiersUni[8] = "\u0DD3";
        vowelsUni[9] = "\u0D8A";
        vowels[9] = "ie";
        vowelModifiersUni[9] = "\u0DD3";
        vowelsUni[10] = "\u0D8A";
        vowels[10] = "ee";
        vowelModifiersUni[10] = "\u0DD3";
        vowelsUni[11] = "\u0D8A";
        vowels[11] = "i\\)";
        vowelModifiersUni[11] = "\u0DD3";
        vowelsUni[12] = "\u0D92";
        vowels[12] = "ea";
        vowelModifiersUni[12] = "\u0DDA";
        vowelsUni[13] = "\u0D92";
        vowels[13] = "ei";
        vowelModifiersUni[13] = "\u0DDA";
        vowelsUni[14] = "\u0D92";
        vowels[14] = "e\\)";
        vowelModifiersUni[14] = "\u0DDA";
        vowelsUni[15] = "\u0D8C";
        vowels[15] = "uu";
        vowelModifiersUni[15] = "\u0DD6";
        vowelsUni[16] = "\u0D8C";
        vowels[16] = "u\\)";
        vowelModifiersUni[16] = "\u0DD6";
        vowelsUni[17] = "\u0D96";
        vowels[17] = "au";
        vowelModifiersUni[17] = "\u0DDE";
        vowelsUni[18] = "\u0D87";
        vowels[18] = "A";
        vowelModifiersUni[18] = "\u0DD0";
        vowelsUni[19] = "\u0D85";
        vowels[19] = "a";
        vowelModifiersUni[19] = "";
        vowelsUni[20] = "\u0D87";
        vowels[20] = "/\\a";
        vowelModifiersUni[20] = "\u0DD0";
        vowelsUni[21] = "\u0D89";
        vowels[21] = "i";
        vowelModifiersUni[21] = "\u0DD2";
        vowelsUni[22] = "\u0D91";
        vowels[22] = "e";
        vowelModifiersUni[22] = "\u0DD9";
        vowelsUni[23] = "\u0D8B";
        vowels[23] = "u";
        vowelModifiersUni[23] = "\u0DD4";
        vowelsUni[24] = "\u0D94";
        vowels[24] = "o";
        vowelModifiersUni[24] = "\u0DDC";
        vowelsUni[25] = "\u0D93";
        vowels[25] = "I";
        vowelModifiersUni[25] = "\u0DDB";
        nVowels = 26;

        specialConsonantsUni[0] = "\u0D82";
        specialConsonants[0] = "\\n";
        specialConsonantsUni[1] = "\u0D83";
        specialConsonants[1] = "\\h";
        specialConsonantsUni[2] = "\u0D9E";
        specialConsonants[2] = "\\N";
        specialConsonantsUni[3] = "\u0D8D";
        specialConsonants[3] = "\\R";
        specialConsonantsUni[4] = "\u0DBB\u0DCA" + "\u200D";
        specialConsonants[4] = "/R";
        specialConsonantsUni[5] = "\u0DBB\u0DCA" + "\u200D";
        specialConsonants[5] = "\\r";

        consonantsUni[0] = "\u0DAC";
        consonants[0] = "nnd";
        consonantsUni[1] = "\u0DB3";
        consonants[1] = "nndh";
        consonantsUni[2] = "\u0D9F";
        consonants[2] = "nng";
        consonantsUni[3] = "\u0DAE";
        consonants[3] = "Th";
        consonantsUni[4] = "\u0DB0";
        consonants[4] = "Dh";
        consonantsUni[5] = "\u0DA5";
        consonants[5] = "GN";
        consonantsUni[6] = "\u0DA1";
        consonants[6] = "Ch";
        consonantsUni[7] = "\u0DB5";
        consonants[7] = "ph";
        consonantsUni[8] = "\u0DB7";
        consonants[8] = "bh";
        consonantsUni[9] = "\u0DC1";
        consonants[9] = "sh";
        consonantsUni[10] = "\u0DC2";
        consonants[10] = "Sh";
        consonantsUni[11] = "\u0D9D";
        consonants[11] = "G";
        consonantsUni[12] = "\u0DA4";
        consonants[12] = "KN";
        consonantsUni[13] = "\u0DC5\u0DD4";
        consonants[13] = "Lu";
        consonantsUni[14] = "\u0DAF";
        consonants[14] = "dh";
        consonantsUni[15] = "\u0DA0";
        consonants[15] = "ch";
        consonantsUni[16] = "\u0D9B";
        consonants[16] = "K";
        consonantsUni[17] = "\u0DAD";
        consonants[17] = "th";
        consonantsUni[18] = "\u0DA7";
        consonants[18] = "t";
        consonantsUni[19] = "\u0D9B";
        consonants[19] = "kh";
        consonantsUni[20] = "\u0DA9";
        consonants[20] = "d";
        consonantsUni[21] = "\u0DB1";
        consonants[21] = "n";
        consonantsUni[22] = "\u0DB4";
        consonants[22] = "p";
        consonantsUni[23] = "\u0DB6";
        consonants[23] = "b";
        consonantsUni[24] = "\u0DB8";
        consonants[24] = "m";
        consonantsUni[25] = "\u0DBA";
        consonants[25] = "y";
        consonantsUni[26] = "\u200D\u0DBA";
        consonants[26] = "Y";
        consonantsUni[27] = "\u0DBA";
        consonants[27] = "\\u005C" + "y";
        consonantsUni[28] = "\u0DA2";
        consonants[28] = "j";
        consonantsUni[29] = "\u0DBD";
        consonants[29] = "l";
        consonantsUni[30] = "\u0DC0";
        consonants[30] = "v";
        consonantsUni[31] = "\u0DC0";
        consonants[31] = "w";
        consonantsUni[32] = "\u0DC3";
        consonants[32] = "s";
        consonantsUni[33] = "\u0DC4";
        consonants[33] = "h";
        consonantsUni[34] = "\u0DAB";
        consonants[34] = "N";
        consonantsUni[35] = "\u0DC5";
        consonants[35] = "L";
        consonantsUni[36] = "\u0D9A";
        consonants[36] = "k";
        consonantsUni[37] = "\u0D9D";
        consonants[37] = "gh";
        consonantsUni[38] = "\u0DA8";
        consonants[38] = "T";
        consonantsUni[39] = "\u0DAA";
        consonants[39] = "D";
        consonantsUni[40] = "\u0DB5";
        consonants[40] = "P";
        consonantsUni[41] = "\u0DB9";
        consonants[41] = "B";
        consonantsUni[42] = "\u0DC6";
        consonants[42] = "f";
        consonantsUni[43] = "\u0DA3";
        consonants[43] = "q";
        consonantsUni[44] = "\u0D9C";
        consonants[44] = "g";
        consonantsUni[45] = "\u0DBB";
        consonants[45] = "r";

        specialCharUni[0] = "\u0DF2";
        specialChar[0] = "ruu";
        specialCharUni[1] = "\u0DD8";
        specialChar[1] = "ru";
        //specialCharUni[2]="???";            specialChar[2]="ra";
    }

    /**
     *
     * @param txt The string to be transliterated
     * @return  The Sinhala transliteration
     */
    public String transliterate(String txt) {
        String s, v;
        Pattern pattern;
        text = txt;

        //special consonents
        for (int i = 0; i < specialConsonants.length; i++) {
            text = text.replace(specialConsonants[i], specialConsonantsUni[i]);
        }

        //consonents + special Chars
        for (int i = 0; i < specialCharUni.length; i++) {
            for (int j = 0; j < consonants.length; j++) {
                s = consonants[j] + specialChar[i];
                v = consonantsUni[j] + specialCharUni[i];
                pattern = Pattern.compile(s);
                text = text.replace(pattern.pattern(), v);
            }
        }

        //consonants + Rakaransha + vowel modifiers
        for (int j = 0; j < consonants.length; j++) {
            for (int i = 0; i < vowels.length; i++) {
                s = consonants[j] + "r" + vowels[i];
                v = consonantsUni[j] + "\u0DCA\u200D\u0DBB" + vowelModifiersUni[i];
                pattern = Pattern.compile(s);
                text = text.replace(pattern.pattern(), v);
            }
            s = consonants[j] + "r";
            v = consonantsUni[j] + "\u200D\u0DCA\u0DBB";
            pattern = Pattern.compile(s);
            text = text.replace(pattern.pattern(), v);
        }
        //consonents + vowel modifiers
        for (int i = 0; i < consonants.length; i++) {
            for (int j = 0; j < nVowels; j++) {
                s = consonants[i] + vowels[j];
                v = consonantsUni[i] + vowelModifiersUni[j];
                pattern = Pattern.compile(s);
                text = text.replace(pattern.pattern(), v);
            }
        }

        //consonents + HAL
        for (int i = 0; i < consonants.length; i++) {
            pattern = Pattern.compile(consonants[i]);
            text = text.replace(pattern.pattern(), consonantsUni[i] + "\u0DCA");
        }

        //vowels
        for (int i = 0; i < vowels.length; i++) {
            pattern = Pattern.compile(vowels[i]);
            text = text.replace(pattern.pattern(), vowelsUni[i]);
        }
        return text;
    }
}
